package vip.xiaonuo.supplier.modular.warehouse.storesstream.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.alibaba.excel.write.style.row.AbstractRowHeightStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fhs.trans.service.impl.TransService;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import jakarta.annotation.Resource;
import vip.xiaonuo.common.util.CommonDownloadUtil;
import vip.xiaonuo.common.util.CommonResponseUtil;
import vip.xiaonuo.supplier.modular.base.productions.entity.SupplierProductions;
import vip.xiaonuo.supplier.modular.base.productions.service.SupplierProductionsService;
import vip.xiaonuo.supplier.modular.warehouse.storesstream.entity.SupplierStoresStream;
import vip.xiaonuo.supplier.modular.warehouse.storesstream.mapper.SupplierStoresStreamMapper;
import vip.xiaonuo.supplier.modular.warehouse.storesstream.param.*;
import vip.xiaonuo.supplier.modular.warehouse.storesstream.result.SupplierStoresStreamExportResult;
import vip.xiaonuo.supplier.modular.warehouse.storesstream.service.SupplierStoresStreamService;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 库存流水Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/03/19 09:49
 **/
@Service
public class SupplierStoresStreamServiceImpl extends ServiceImpl<SupplierStoresStreamMapper, SupplierStoresStream> implements SupplierStoresStreamService {

    @Resource
    private SupplierProductionsService supplierProductionsService;

    @Resource
    private TransService transService;
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public Page<SupplierStoresStream> page(SupplierStoresStreamPageParam supplierStoresStreamPageParam) {
        QueryWrapper<SupplierStoresStream> queryWrapper = new QueryWrapper<SupplierStoresStream>().checkSqlInjection();

        if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getSearchKey())) {
            List<String> ids = CollStreamUtil.toList(supplierProductionsService.list(new QueryWrapper<SupplierProductions>().select("ID").lambda().eq(SupplierProductions::getCompanyId,StpLoginUserUtil.getLoginUser().getCompanyId()).and(i->i.like(SupplierProductions::getProductionName,supplierStoresStreamPageParam.getSearchKey())
                    .or().like(SupplierProductions::getProductionPinyincode,supplierStoresStreamPageParam.getSearchKey())
                    .or().like(SupplierProductions::getProductionCode,supplierStoresStreamPageParam.getSearchKey())
                    .or().like(SupplierProductions::getProductionBarcode,supplierStoresStreamPageParam.getSearchKey()))), SupplierProductions::getId);
            if(ObjectUtil.isNotEmpty(ids)) {
                queryWrapper.lambda().in(SupplierStoresStream::getStreamProductionId,ids);
            }else{
                return new Page<>();
            }
        }
        if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getSearchKey())) {
            queryWrapper.lambda().and(i -> i.like(SupplierStoresStream::getStreamProductionBarcode,supplierStoresStreamPageParam.getSearchKey())
                    .or().like(SupplierStoresStream::getStreamProductionName, supplierStoresStreamPageParam.getSearchKey())
            );
        }
        if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getStreamProductionBarcode())) {
            queryWrapper.lambda().eq(SupplierStoresStream::getStreamProductionBarcode, supplierStoresStreamPageParam.getStreamProductionBarcode());
        }
        if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getStreamProductionName())) {
            queryWrapper.lambda().like(SupplierStoresStream::getStreamProductionName, supplierStoresStreamPageParam.getStreamProductionName());
        }
        if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getStreamStoreId())) {
            queryWrapper.lambda().and(i -> i.eq(SupplierStoresStream::getStreamStoreId, supplierStoresStreamPageParam.getStreamStoreId()).or().eq(SupplierStoresStream::getStreamStorageId, supplierStoresStreamPageParam.getStreamStoreId()));
        }
        if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getStreamBusinessType())) {
            queryWrapper.lambda().eq(SupplierStoresStream::getStreamBusinessType, supplierStoresStreamPageParam.getStreamBusinessType());
        }
        if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getStreamObjectId())) {
            queryWrapper.lambda().eq(SupplierStoresStream::getStreamObjectId, supplierStoresStreamPageParam.getStreamObjectId());
        }
        //查询时间范围
        if (ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getStartCreateTime()) && ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getEndCreateTime())) {
            queryWrapper.lambda().between(SupplierStoresStream::getCreateTime, supplierStoresStreamPageParam.getStartCreateTime() + " 00:00:01", supplierStoresStreamPageParam.getEndCreateTime() + " 23:59:59");
        }
        //勾选汇总选项
        if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getIsHavingByProduction()) && supplierStoresStreamPageParam.getIsHavingByProduction()){
            String sqlString = "STREAM_PRODUCTION_ID , STREAM_PRODUCTION_CODE,STREAM_PRODUCTION_BARCODE,STREAM_PRODUCTION_NAME,STREAM_PRODUCTION_SPEC,STREAM_PRODUCTION_UNIT, SUM(STREAM_IN_NUMBER) as STREAM_IN_NUMBER, SUM(STREAM_OUT_NUMBER) as STREAM_OUT_NUMBER";
            String groupString = "STREAM_PRODUCTION_ID,STREAM_PRODUCTION_CODE,STREAM_PRODUCTION_BARCODE,STREAM_PRODUCTION_NAME,STREAM_PRODUCTION_SPEC,STREAM_PRODUCTION_UNIT";

            if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getIsHavingByStore()) && supplierStoresStreamPageParam.getIsHavingByStore()) {
                sqlString = sqlString + ", STREAM_STORE_ID";
                groupString = groupString + ", STREAM_STORE_ID";
            }
            if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getIsHavingByStorage()) && supplierStoresStreamPageParam.getIsHavingByStorage()) {
                sqlString = sqlString + ", STREAM_STORAGE_ID";
                groupString = groupString + ", STREAM_STORAGE_ID";
            }
            if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getIsHavingByStoreLocation()) && supplierStoresStreamPageParam.getIsHavingByStoreLocation()) {
                sqlString = sqlString + ", STREAM_STORE_LOCATION";
                groupString = groupString + ", STREAM_STORE_LOCATION";
            }
            if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getIsHavingByProductionBatch()) && supplierStoresStreamPageParam.getIsHavingByProductionBatch()) {
                sqlString = sqlString + ", STREAM_PRODUCTION_BATCH";
                groupString = groupString + ", STREAM_PRODUCTION_BATCH";
            }
            if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getIsHavingByDateManufacture()) && supplierStoresStreamPageParam.getIsHavingByDateManufacture()) {
                sqlString = sqlString + ", STREAM_DATE_MANUFACTURE";
                groupString = groupString + ", STREAM_DATE_MANUFACTURE";
            }
            if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getIsHavingBySupplier()) && supplierStoresStreamPageParam.getIsHavingBySupplier()) {
                sqlString = sqlString + ", STREAM_OBJECT_ID";
                groupString = groupString + ", STREAM_OBJECT_ID";
            }
            if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getIsHavingByBusinessType()) && supplierStoresStreamPageParam.getIsHavingByBusinessType()) {
                sqlString = sqlString + ", STREAM_BUSINESS_TYPE";
                groupString = groupString + ", STREAM_BUSINESS_TYPE";
            }
            queryWrapper.select(sqlString);
            queryWrapper.groupBy(groupString);
        }

        //数据归属
        queryWrapper.lambda().eq(SupplierStoresStream::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierStoresStreamPageParam.getSortField(), supplierStoresStreamPageParam.getSortOrder())) {
            System.out.println(supplierStoresStreamPageParam.getSortField());
            System.out.println(supplierStoresStreamPageParam.getSortOrder());
            CommonSortOrderEnum.validate(supplierStoresStreamPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierStoresStreamPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierStoresStreamPageParam.getSortField()));
        } else {
            if(ObjectUtil.isNotEmpty(supplierStoresStreamPageParam.getIsHavingByProduction()) && supplierStoresStreamPageParam.getIsHavingByProduction()){
                supplierStoresStreamPageParam.setSortField("streamProductionId");
                supplierStoresStreamPageParam.setSortOrder("ascend");
            }else{
                queryWrapper.lambda().orderByAsc(SupplierStoresStream::getId);
            }
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierStoresStreamAddParam supplierStoresStreamAddParam) {
        SupplierStoresStream supplierStoresStream = BeanUtil.toBean(supplierStoresStreamAddParam, SupplierStoresStream.class);
        //数据归属
        supplierStoresStream.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierStoresStream);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierStoresStreamEditParam supplierStoresStreamEditParam) {
        SupplierStoresStream supplierStoresStream = this.queryEntity(supplierStoresStreamEditParam.getId());
        BeanUtil.copyProperties(supplierStoresStreamEditParam, supplierStoresStream);
        this.updateById(supplierStoresStream);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierStoresStreamIdParam> supplierStoresStreamIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierStoresStreamIdParamList, SupplierStoresStreamIdParam::getId));
    }

    @Override
    public SupplierStoresStream detail(SupplierStoresStreamIdParam supplierStoresStreamIdParam) {
        return this.queryEntity(supplierStoresStreamIdParam.getId());
    }

    @Override
    public SupplierStoresStream queryEntity(String id) {
        SupplierStoresStream supplierStoresStream = this.getById(id);
        if(ObjectUtil.isEmpty(supplierStoresStream)) {
            throw new CommonException("库存流水不存在，id值为：{}", id);
        }
        return supplierStoresStream;
    }

    @Override
    public void exportStream(SupplierStoresStreamExportParam supplierStoresStreamExportParam, HttpServletResponse response) throws IOException {
        File tempFile = null;
        try {
                QueryWrapper<SupplierStoresStream> queryWrapper = new QueryWrapper<SupplierStoresStream>().checkSqlInjection();
                //如果是勾选的记录
                if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getIds())) {
                    if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getIsHavingByProduction()) && supplierStoresStreamExportParam.getIsHavingByProduction()){
//                        queryWrapper.lambda().in(SupplierStoresStream::getStreamProductionId, supplierStoresStreamExportParam.getStreamProductionIds().split(","));
                    }else{
                        queryWrapper.lambda().in(SupplierStoresStream::getId, supplierStoresStreamExportParam.getIds().split(","));
                    }

                }

                //关键字查询
                if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getProductionSearchKey())) {
                    List<String> ids = CollStreamUtil.toList(supplierProductionsService.list(new QueryWrapper<SupplierProductions>().select("ID").lambda().eq(SupplierProductions::getCompanyId,StpLoginUserUtil.getLoginUser().getCompanyId()).and(i->i.like(SupplierProductions::getProductionName,supplierStoresStreamExportParam.getProductionSearchKey())
                            .or().like(SupplierProductions::getProductionPinyincode,supplierStoresStreamExportParam.getProductionSearchKey())
                            .or().like(SupplierProductions::getProductionCode,supplierStoresStreamExportParam.getProductionSearchKey())
                            .or().like(SupplierProductions::getProductionBarcode,supplierStoresStreamExportParam.getProductionSearchKey()))), SupplierProductions::getId);
                    if(ObjectUtil.isNotEmpty(ids)) {
                        queryWrapper.lambda().in(SupplierStoresStream::getStreamProductionId,ids);
                    }else{
                        throw new CommonException("没有查到符合条件的数据！");
                    }
                }
                if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getSearchKey())) {
                    queryWrapper.lambda().and(i -> i.like(SupplierStoresStream::getStreamProductionBarcode,supplierStoresStreamExportParam.getSearchKey())
                            .or().like(SupplierStoresStream::getStreamProductionName, supplierStoresStreamExportParam.getStreamProductionName())
                    );
                }
                if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getStreamProductionBarcode())) {
                    queryWrapper.lambda().eq(SupplierStoresStream::getStreamProductionBarcode, supplierStoresStreamExportParam.getStreamProductionBarcode());
                }
                if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getStreamProductionName())) {
                    queryWrapper.lambda().like(SupplierStoresStream::getStreamProductionName, supplierStoresStreamExportParam.getStreamProductionName());
                }
                if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getStreamStoreId())) {
                    queryWrapper.lambda().and(i -> i.eq(SupplierStoresStream::getStreamStoreId, supplierStoresStreamExportParam.getStreamStoreId()).or().eq(SupplierStoresStream::getStreamStorageId, supplierStoresStreamExportParam.getStreamStoreId()));
                }
                if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getStreamBusinessType())) {
                    queryWrapper.lambda().eq(SupplierStoresStream::getStreamBusinessType, supplierStoresStreamExportParam.getStreamBusinessType());
                }
                if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getStreamObjectId())) {
                    queryWrapper.lambda().eq(SupplierStoresStream::getStreamObjectId, supplierStoresStreamExportParam.getStreamObjectId());
                }
                //查询时间范围
                if (ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getStartCreateTime()) && ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getEndCreateTime())) {
                    queryWrapper.lambda().between(SupplierStoresStream::getCreateTime, supplierStoresStreamExportParam.getStartCreateTime() + " 00:00:01", supplierStoresStreamExportParam.getEndCreateTime() + " 23:59:59");
                }
                //勾选汇总选项
                if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getIsHavingByProduction()) && supplierStoresStreamExportParam.getIsHavingByProduction()){
                    String sqlString = "STREAM_PRODUCTION_ID,STREAM_PRODUCTION_CODE,STREAM_PRODUCTION_BARCODE,STREAM_PRODUCTION_NAME,STREAM_PRODUCTION_SPEC,STREAM_PRODUCTION_UNIT, SUM(STREAM_IN_NUMBER) as STREAM_IN_NUMBER, SUM(STREAM_OUT_NUMBER) as STREAM_OUT_NUMBER";
                    String groupString = "STREAM_PRODUCTION_ID,STREAM_PRODUCTION_CODE,STREAM_PRODUCTION_BARCODE,STREAM_PRODUCTION_NAME,STREAM_PRODUCTION_SPEC,STREAM_PRODUCTION_UNIT";

                    if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getIsHavingByStore()) && supplierStoresStreamExportParam.getIsHavingByStore()) {
                        sqlString = sqlString + ", STREAM_STORE_ID";
                        groupString = groupString + ", STREAM_STORE_ID";
                    }
                    if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getIsHavingByStorage()) && supplierStoresStreamExportParam.getIsHavingByStorage()) {
                        sqlString = sqlString + ", STREAM_STORAGE_ID";
                        groupString = groupString + ", STREAM_STORAGE_ID";
                    }
                    if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getIsHavingByStoreLocation()) && supplierStoresStreamExportParam.getIsHavingByStoreLocation()) {
                        sqlString = sqlString + ", STREAM_STORE_LOCATION";
                        groupString = groupString + ", STREAM_STORE_LOCATION";
                    }
                    if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getIsHavingByProductionBatch()) && supplierStoresStreamExportParam.getIsHavingByProductionBatch()) {
                        sqlString = sqlString + ", STREAM_PRODUCTION_BATCH";
                        groupString = groupString + ", STREAM_PRODUCTION_BATCH";
                    }
                    if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getIsHavingByDateManufacture()) && supplierStoresStreamExportParam.getIsHavingByDateManufacture()) {
                        sqlString = sqlString + ", STREAM_DATE_MANUFACTURE";
                        groupString = groupString + ", STREAM_DATE_MANUFACTURE";
                    }
                    if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getIsHavingBySupplier()) && supplierStoresStreamExportParam.getIsHavingBySupplier()) {
                        sqlString = sqlString + ", STREAM_OBJECT_ID";
                        groupString = groupString + ", STREAM_OBJECT_ID";
                    }
                    if(ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getIsHavingByBusinessType()) && supplierStoresStreamExportParam.getIsHavingByBusinessType()) {
                        sqlString = sqlString + ", STREAM_BUSINESS_TYPE";
                        groupString = groupString + ", STREAM_BUSINESS_TYPE";
                    }
                    queryWrapper.select(sqlString);
                    queryWrapper.groupBy(groupString);
                }
                //数据归属
                queryWrapper.lambda().eq(SupplierStoresStream::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
                if(ObjectUtil.isAllNotEmpty(supplierStoresStreamExportParam.getSortField(), supplierStoresStreamExportParam.getSortOrder())) {
                    CommonSortOrderEnum.validate(supplierStoresStreamExportParam.getSortOrder());
                    queryWrapper.orderBy(true, supplierStoresStreamExportParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                            StrUtil.toUnderlineCase(supplierStoresStreamExportParam.getSortField()));
                } else {
                    queryWrapper.lambda().orderByAsc(SupplierStoresStream::getStreamProductionId);
                }
                String fileName = (ObjectUtil.isNotEmpty(supplierStoresStreamExportParam.getIsHavingByProduction()) && supplierStoresStreamExportParam.getIsHavingByProduction()) ? "库存流水（" + supplierStoresStreamExportParam.getStartCreateTime() + "至" + supplierStoresStreamExportParam.getEndCreateTime() + "汇总）.xlsx" :  "库存流水（" + supplierStoresStreamExportParam.getStartCreateTime() + "至" + supplierStoresStreamExportParam.getEndCreateTime() + "明细）.xlsx";
                List<SupplierStoresStream> supplierStoresStreamList = this.list(queryWrapper);
                if(ObjectUtil.isEmpty(supplierStoresStreamList)) {
                    throw new CommonException("没有符合条件的数据可导出");
                }
                transService.transBatch(supplierStoresStreamList);
                supplierStoresStreamList = CollectionUtil.sort(supplierStoresStreamList, Comparator.comparing(SupplierStoresStream::getStreamProductionId));
                List<SupplierStoresStreamExportResult> supplierStoresStreamExportResults = supplierStoresStreamList.stream()
                        .map(supplierStoresStream -> {
                            SupplierStoresStreamExportResult supplierStoresStreamExportResult = new SupplierStoresStreamExportResult();
                            BeanUtil.copyProperties(supplierStoresStream, supplierStoresStreamExportResult);
                            if(ObjectUtil.isNotEmpty(supplierStoresStream.getStreamDateManufacture())){
                                supplierStoresStreamExportResult.setStreamDateManufacture(dateFormat.format(supplierStoresStream.getStreamDateManufacture()));
                            }
                            return supplierStoresStreamExportResult;
                        }).collect(Collectors.toList());
                //创建临时文件
            tempFile = FileUtil.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + fileName);
            // 头的策略
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            WriteFont headWriteFont = new WriteFont();
            headWriteFont.setFontHeightInPoints((short) 14);
            headWriteCellStyle.setWriteFont(headWriteFont);
            // 水平垂直居中
            headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            // 内容的策略
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
            contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
            // 内容背景白色
            contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            WriteFont contentWriteFont = new WriteFont();

            // 内容字体大小
            contentWriteFont.setFontHeightInPoints((short) 10);
            contentWriteCellStyle.setWriteFont(contentWriteFont);

            //设置边框样式，细实线
            contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
            contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
            contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
            contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);

            // 水平垂直居中
            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
            contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle,
                    contentWriteCellStyle);

            // 写excel
            EasyExcel.write(tempFile.getPath(), SupplierStoresStreamExportResult.class)
                    // 自定义样式
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    // 自动列宽
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    // 设置第一行字体
                    .registerWriteHandler(new CellWriteHandler() {
                        @Override
                        public void afterCellDispose(CellWriteHandlerContext context) {
                            WriteCellData<?> cellData = context.getFirstCellData();
                            WriteCellStyle writeCellStyle = cellData.getOrCreateStyle();
                            Integer rowIndex = context.getRowIndex();
                            if(rowIndex == 0) {
                                WriteFont headWriteFont = new WriteFont();
                                headWriteFont.setFontName("宋体");
                                headWriteFont.setBold(true);
                                headWriteFont.setFontHeightInPoints((short) 11);
                                writeCellStyle.setWriteFont(headWriteFont);
                            }
                        }
                    })
                    // 设置表头行高
                    .registerWriteHandler(new AbstractRowHeightStyleStrategy() {
                        @Override
                        protected void setHeadColumnHeight(Row row, int relativeRowIndex) {
                            if(relativeRowIndex == 0) {
                                // 表头第一行
                                row.setHeightInPoints(20);
                            } else {
                                // 表头其他行
                                row.setHeightInPoints(20);
                            }
                        }
                        @Override
                        protected void setContentColumnHeight(Row row, int relativeRowIndex) {
                            // 内容行
                            row.setHeightInPoints(18);
                        }
                    })
                    .sheet("库存明细")
                    .doWrite(supplierStoresStreamExportResults);
            CommonDownloadUtil.download(tempFile, response);
        } catch (Exception e) {
            log.error(">>> 库存流水导出异常：", e);
            CommonResponseUtil.renderError(response, "导出失败");
        } finally {
            FileUtil.del(tempFile);
        }

    }
}
